package ext.tianma.part.Interface.MDMCheckInterface.Service;

import java.io.File;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.alibaba.fastjson.JSON;
import ext.generic.erp.common.CommonUtil;
import ext.tianma.bom.util.ClassificationUtil;
import ext.tianma.bom.util.StringUtil;
import ext.tianma.part.Interface.MDMCheckInterface.Bean.PartCheckBean;
import ext.tianma.part.Interface.MDMCheckInterface.Bean.PartCheckInformationSendBean;
import ext.tianma.part.Interface.MDMCheckInterface.Bean.SendBean;
import ext.tianma.part.Interface.Until.ClassificationMDM;
import ext.tianma.util.PropertiesUtil;
import wt.fc.WTObject;
import wt.part.WTPart;
import wt.type.ClientTypedUtility;
import wt.util.WTException;

/**
 * 用于封装部件信息,进行发送json
 * 
 * @author laichao_sun
 *
 */
public class PartCheckService {

	public static String path = "codebase" + File.separator + "ext" + File.separator + "tianma" + File.separator
			+ "part" + File.separator + "config" + File.separator + "MDMAttribute.properties";

	public static boolean sendCheckService(WTObject obj) throws RemoteException, WTException {
		System.out.println("SLC ==进入MDM校验接口==>");
		try {
			if (obj instanceof WTPart) {
				PropertiesUtil ut = new PropertiesUtil(path);

				PartCheckBean partBean = new PartCheckBean();

				WTPart part = (WTPart) obj;
				// 名称
				partBean.setPartName(part.getName());
				// 料号
				partBean.setPartNumber(part.getNumber());
				// 物料类别
				String partType = ClientTypedUtility.getLocalizedTypeName(part, Locale.CHINA);
				partBean.setPartType(partType);
				// 分类路径对应的MDM的分类代码
				String classification = ClassificationUtil.getClassification(part);
				classification = replaceBlank(classification);
				System.out.println("SLC ==分类路径==>" + classification+" | "+part.getNumber()+" | "+part.getViewName());
				String code = ut.getValueByKey(classification);
				if (StringUtil.isNullOrEmpty(code)) {
					throw new WTException("MDM的分类对应的值为空!请确认所选的分类是否正确!");
				}
				partBean.setClassification(code);
				// 分类属性集合
				List<HashMap<String, String>> list2 = ClassificationMDM.getClassifictionIba(ClassificationUtil.getClassificationNode(part).getName(),part);
				Map<String, String> maps = new HashMap<String, String>();
				for (HashMap<String, String> map : list2) {
					String displayname = map.get("displayname");
					String value = map.get("value");
					String key = map.get("key");
					//System.out.println("SLC ==MDM传递的分类属性==>displayname: "+displayname+" | key:"+key+" | value:"+value);
					maps.put(key, value==null?"":value);
				}
				partBean.setClassificationList(maps);

				SendBean sendBean = new SendBean();
				sendBean.setParts(partBean);
				// 封装发送信息
				PartCheckInformationSendBean check = new PartCheckInformationSendBean();
				check.setServiceInterfaceCode("ESBSDPH0005");
				check.setFromSystemCode("101SDP");
				check.setFromInterfaceCode("");
				check.setToInterfaceCode("MDMH0002");
				check.setOuZone("OU_TMSH");
				check.setFactoryName("");
				check.setUuid("653ab555e56909e754f0b1c359283069");
				String time = CommonUtil.getCurrentTime();
				check.setRequestTime(time);
				check.setUserName("");
				check.setPassword("");
				check.setSign("");
				check.setDataType("JSON");
				check.setData(sendBean);

				String result2 = JSON.toJSONString(check);

				// url post发布物料信息
				String result = PartCheckServiceToSOA.sendPartCheckBean(result2);
				System.out.println("SLC ==调用接口返回的结果==> " + result);
				if (!StringUtil.isNullOrEmpty(result)) {
					// 二级物料接口返回值的校验
					JSONObject object;
					object = new JSONObject((String) result.toString());
					System.out.println("返回值:" + object);
					if (object.has("resultCode")) {

						/*
						 * {"resultCode":"1","resultMsg":null}
						 */
						String resultCode = object.getString("resultCode");
						String resultMsg = object.getString("resultMsg");

						if ("1".equals(resultCode)) {

						} else {
							JSONObject resultData = object.getJSONObject("resultData");
							if (!(resultData == null)) {
								JSONArray dataInfo = resultData.getJSONArray("dataInfo");
								String string = "";
								for (int i = 0; i < dataInfo.length(); i++) {
									JSONObject ob = (JSONObject) dataInfo.get(i);
									if (ob.has("synResult")) {
										String synResult = ob.getString("synResult");
										string = string + synResult;
									}
								}
								if(!StringUtil.isNullOrEmpty(string)) {
									throw new WTException(string);									
								}
							}
						}
					}
					return true;
				} else {
					throw new WTException("调用MDM接口:主数据校验数据接口失败!");
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			throw new WTException("调用MDM接口:主数据校验数据接口失败!");
		}
		return false;
	}

	// 去除所有空白格
	public static String replaceBlank(String str) {
		String dest = null;
		if (str == null) {
			return dest;
		} else {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n|&nbsp;");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
			return dest;

		}

	}
}
